//
//  Common.swift
//  YMPayUI
//
//  Created by youmy on 2018/11/19.
//  Copyright © 2018 youmyyoumy. All rights reserved.
//

import Foundation
import UIKit

/// 屏幕尺寸
let ym_screen_bounds = UIScreen.main.bounds

/// 屏幕大小
let ym_screen_size = UIScreen.main.bounds.size

/// 屏幕宽度
let ym_screen_width = UIScreen.main.bounds.width

/// 屏幕高度
let ym_screen_height = UIScreen.main.bounds.height

// 字体
/// 苹方-Regular
let PingFangSC_Regular = "PingFangSC-Regular"

/// 苹方-Medium
let PingFangSC_Medium = "PingFangSC-Medium"

let bundle = Bundle(path: Bundle.main.path(forResource: "RentFreeImages.bundle", ofType: nil) ?? "")


func resoucearrayFromFile(fileName: String, bundle:Bundle? = nil) -> [[String:Any]] {
    
    var jsonPath:String?
    
    if bundle != nil {
        jsonPath = bundle?.path(forResource: fileName, ofType: nil)
    }else{
        jsonPath = Bundle.main.path(forResource: fileName, ofType: nil)
    }
    
    guard let path = jsonPath else {
        return [[String: Any]]()
    }
    
    let data = try? Data(contentsOf: URL(fileURLWithPath: path))
    
    if data != nil {
        let arr = try? JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as! Array<Dictionary<String, Any>>
        return arr ?? [[String: Any]]()
    }
    return [[String: Any]]()
}


///获取当前控制器
func currentVc() ->UIViewController{
    
    var vc = UIApplication.shared.keyWindow?.rootViewController
    
    if (vc?.isKind(of: UITabBarController.self))! {
        vc = (vc as! UITabBarController).selectedViewController
    }else if (vc?.isKind(of: UINavigationController.self))!{
        vc = (vc as! UINavigationController).visibleViewController
    }else if ((vc?.presentedViewController) != nil){
        vc =  vc?.presentedViewController
    }
    
    return vc!
    
}


extension String {
    
    /// 获取字符串宽度
    ///
    /// - Parameters:
    ///   - font: 字体
    ///   - height: 高度
    /// - Returns: 宽度
    func getWidth(font: UIFont, height: CGFloat = 15) -> CGFloat {
        let rect = NSString(string: self).boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: height), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
        return ceil(rect.width)
    }
    
    
    /// 获取字符串高度
    ///
    /// - Parameters:
    ///   - font: 字体
    ///   - width: 宽度
    /// - Returns: 高度
    func getHeight(font: UIFont, width: CGFloat) -> CGFloat {
        let rect = NSString(string: self).boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
        return ceil(rect.height)
    }
    
    
    /// 获取字符串高度
    ///
    /// - Parameters:
    ///   - font: 字体
    ///   - width: 宽度
    ///   - maxHeight: 最高高度
    /// - Returns: 高度
    func getHeight(font: UIFont, width: CGFloat, maxHeight: CGFloat) -> CGFloat {
        let rect = NSString(string: self).boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
        return ceil(rect.height)>maxHeight ? maxHeight : ceil(rect.height)
    }
}
